package com.demo.controller;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.domain.SysPrivilege;
import com.demo.model.R;
import com.demo.service.SysPrivilegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;

/**
 * 1 查询方法  sys_privilege_query
 * 2 修改     sys_privilege_update
 * 3 增加     sys_privilege_create
 * 4 删除     sys_privilege_delete
 * 权限管理的权限
 */
@RestController
@RequestMapping("/privileges")
@Api(tags = "权限的管理")
public class SysPrivilegeController {

    /**
     * 查询接口 若用户具有查询权限 则可进行查询动作
     */
    @Autowired
    private SysPrivilegeService sysPrivilegeService;

    @GetMapping
    @ApiOperation(value = "分页查询权限数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current" ,value = "当前页") ,
            @ApiImplicitParam(name = "size" ,value = "每页显示的大小") ,
    })
    @PreAuthorize("hasAuthority('sys_privilege_query')")
    public R<Page<SysPrivilege>> findByPage(@ApiIgnore Page<SysPrivilege> page){

        page.addOrder(OrderItem.desc("last_update_time"));
        Page<SysPrivilege> sysPrivilegePage = sysPrivilegeService.page(page);
            return R.ok(sysPrivilegePage);
    }


    /**
     * 新增接口 若用户具有新增权限 则可进行新增动作
     */
    @PostMapping
    @ApiOperation(value = "新增一个权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "SysPrivilege",value = "SysPrivilege的json数据")
    })
    @PreAuthorize("hasAuthority('sys_privilege_create')")
    public R add(@RequestBody @Valid SysPrivilege sysPrivilege){

        //新增时需要给新增对象补充属性
//        String userIdStr = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
//        sysPrivilege.setCreateBy(Long.valueOf(userIdStr));
//        sysPrivilege.setCreated(new Date());
//        sysPrivilege.setLastUpdateTime(new Date());
        boolean save = sysPrivilegeService.save(sysPrivilege);
        if (save){
            return R.ok("权限新增成功");
        }else{
            return R.fail("权限新增失败");
        }
    }

    /**
     * 修改接口 若用户具有修改权限 则可进行修改动作
     */
    @PatchMapping
    @ApiOperation(value = "修改权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "SysPrivilege",value = "SysPrivilege的json数据")
    })
    @PreAuthorize("hasAuthority('sys_privilege_update')")
    public R update(@RequestBody @Valid SysPrivilege sysPrivilege){

        //新增时需要给新增对象补充属性
        String userIdStr = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        sysPrivilege.setModifyBy(Long.valueOf(userIdStr));
        sysPrivilege.setLastUpdateTime(new Date());
        boolean update = sysPrivilegeService.updateById(sysPrivilege);
        if (update){
            return R.ok("用户{"+userIdStr+"}修改权限成功");
        }else{
            return R.fail("用户{"+userIdStr+"}修改权限失败");
        }
    }
}
